/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.data.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PostLoad;
import javax.persistence.Temporal;
import javax.persistence.Transient;


/**
 * This Entity class represent assignment which will be submitted. The
 * dateOfSubmit is transient attribute and is posloaded based on actual version.
 *
 * @author Tom
 * @version 1.0.0 beta
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "AssignmentSubmit.findByStudent", query = "SELECT a FROM AssignmentSubmit a WHERE a.student = :student"),})
public class AssignmentSubmit implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private static final long serialVersionUID = 1L;
    @OneToMany(mappedBy = "assignmentSubmit", fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
     @OrderBy(value = "numberOfVersion DESC")
    private List<Version> mVersions;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    private Assignment assignment;
    private int grade;
    @Lob
    private String evaluation;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    private Student student;
    @Transient
    private Date dateOfSubmit;
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date dateOfEvaluation;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    private Team team;

    @PostLoad
    public void loadDate() {
        List<Version> versionsOfTask = getmVersions();
        if (versionsOfTask != null) {
            if (!versionsOfTask.isEmpty()) {
                Iterator vIterator = versionsOfTask.iterator();
                Date maxDate = new Date(0);
                while (vIterator.hasNext()) {
                    Version currVersion = (Version) vIterator.next();
                    currVersion.getDateOfSubmit();
                    if (maxDate.before(currVersion.getDateOfSubmit())) {
                        maxDate = currVersion.getDateOfSubmit();
                    }
                }
                setDateOfSubmit(maxDate);
            }
        }
    }

    public AssignmentSubmit() {
    }

    public Date getDateOfEvaluation() {
        return dateOfEvaluation;
    }

    public void setDateOfEvaluation(Date dateOfEvaluation) {
        this.dateOfEvaluation = dateOfEvaluation;
    }

    public Date getDateOfSubmit() {

        return dateOfSubmit;
    }

    public void setDateOfSubmit(Date dateOfSubmit) {
        this.dateOfSubmit = dateOfSubmit;
    }
    @Column(name = "stateOfTask")
    private State stateOfTask;

    public static enum State {

        REJECTED,
        SUBMITTED,
        ACCEPTED,
    };

    public AssignmentSubmit(Assignment assignment, Student student) {
        this.assignment = assignment;
        this.student = student;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public State getStateOfTask() {
        return stateOfTask;
    }

    public void setStateOfTask(State stateOfTask) {
        this.stateOfTask = stateOfTask;
    }

    public Assignment getAssignment() {
        return assignment;
    }

    public void setAssignment(Assignment assignment) {
        this.assignment = assignment;
    }

    public String getEvaluation() {
        return evaluation;
    }

    public void setEvaluation(String evaluation) {
        this.evaluation = evaluation;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof AssignmentSubmit)) {
            return false;
        }
        AssignmentSubmit other = (AssignmentSubmit) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "DL.entity.Assignment_submit[ id=" + id + " ]";
    }
   
    public List<Version> getmVersions() {
        return mVersions;
    }

    public void setmVersions(List<Version> mVersions) {
        this.mVersions = mVersions;
    }

    public void addVersion(Version newVersion) {
        if (mVersions == null) {
            mVersions = new ArrayList<Version>();
        }
        mVersions.add(newVersion);
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }
}
